<?php
class CSearch
{
	public function search($key,$limit, $isCount = 0)
	{
		require_once MY_ROOT . '/library/sphinxapi.php';
        $cl = new SphinxClient ();
        $cl->SetServer ('/var/run/searchd.sock');
        //$cl->SetServer ('localhost', 9312);
        $cl->SetConnectTimeout ( 2 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_ALL );
        $cl->SetWeights(array( 100, 1 ));
        //$cl->SetIndexWeights(array('product'=>10000, 'crawl'=>1));

        $cl->SetLimits(0,  $limit);
    
        $res = $cl->Query ($key, 'crawl');
        if ($res['total_found'] == 0) {
            return 0;
        }
        
        $return = array();
        if(is_array($res['matches'])){
	        foreach ($res['matches'] as $v) {           
	            $return[] = $v['id'];
	        }
        }
        return $return;		
	}
	
	public function searchCrawlAny($key,$limit, $isCount = 0)
	{
		require_once MY_ROOT . '/library/sphinxapi.php';
        $cl = new SphinxClient ();
        $cl->SetServer ('/var/run/searchd.sock');
        //$cl->SetServer ('localhost', 9312);
        $cl->SetConnectTimeout ( 2 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_ANY );
        $cl->SetWeights(array( 100, 1 ));
        //$cl->SetIndexWeights(array('product'=>10000, 'crawl'=>1));

        $cl->SetLimits(0,  $limit);
    
        $res = $cl->Query ($key, 'crawl');
        if ($res['total_found'] == 0) {
            return 0;
        }
        
        $return = array();
        if(is_array($res['matches'])){
	        foreach ($res['matches'] as $v) {           
	            $return[] = $v['id'];
	        }
        }
        return $return;		
	}
	
	public function searchProduct($key)
	{
		require_once MY_ROOT . '/library/sphinxapi.php';
        $cl = new SphinxClient ();
        $cl->SetServer ('/var/run/searchd.sock');
        //$cl->SetServer ('localhost', 9312);
        $cl->SetConnectTimeout ( 2 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_ALL );
        $cl->SetWeights(array( 100, 1 ));
        //$cl->SetIndexWeights(array('product'=>10000, 'crawl'=>1));
    
        $res = $cl->Query ($key, 'product');
        if ($res['total_found'] == 0) {
            return 0;
        }
        
        $return = array();
        if(is_array($res['matches'])){
	        foreach ($res['matches'] as $v) {           
	            $return[] = $v['id'];
	        }
        }
        return $return;		
	}
	
	public function searchProduct2($key)
	{
		require_once MY_ROOT . '/library/sphinxapi.php';
        $cl = new SphinxClient ();
        $cl->SetServer ('/var/run/searchd.sock');
        //$cl->SetServer ('localhost', 9312);
        $cl->SetConnectTimeout ( 2 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_ANY );
        $cl->SetWeights(array( 100, 1 ));
        //$cl->SetIndexWeights(array('product'=>10000, 'crawl'=>1));
    
        $res = $cl->Query ($key, 'product');
        if ($res['total_found'] == 0) {
            return 0;
        }
        
        $return = array();
        if(is_array($res['matches'])){
        	foreach ($res['matches'] as $v) {           
            	$return[] = $v['id'];
        	}
        }
        return $return;		
	}
	
	public function search2($key, $page, $limit, $isCount = 0)
	{
		require_once MY_ROOT . '/library/sphinxapi.php';
        $cl = new SphinxClient ();
        $cl->SetServer ('/var/run/searchd.sock');
        //$cl->SetServer ('localhost', 9312);
        $cl->SetConnectTimeout ( 2 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_ALL );
        $cl->SetWeights(array( 100, 1 ));
        $cl->SetIndexWeights(array('product'=>100, 'crawl'=>1));
		$start = ($page - 1) * $limit;
		
        $cl->SetLimits($start,  $limit);
        $res = $cl->Query ($key, 'product,crawl');
        if ($res['total_found'] == 0) {
            return 0;
        }
        
        if ($isCount) {
        	return $res['total_found'];
        }
        
        $return = array();
        if(is_array($res['matches'])){
        	foreach ($res['matches'] as $v) {           
	            $return[] = $v;
	        }
        }
        return $return;		
	}
	
	public function search3($key, $page, $limit, $isCount = 0)
	{
		require_once MY_ROOT . '/library/sphinxapi.php';
        $cl = new SphinxClient ();
        $cl->SetServer ('/var/run/searchd.sock');
        //$cl->SetServer ('localhost', 9312);
        $cl->SetConnectTimeout ( 2 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_ANY );
        $cl->SetWeights(array( 100, 1 ));
        $cl->SetIndexWeights(array('product'=>100, 'crawl'=>1));
		$start = ($page - 1) * $limit;
		
        $cl->SetLimits($start,  $limit);
        $res = $cl->Query ($key, 'product,crawl');
        if ($res['total_found'] == 0) {
            return 0;
        }
        
        if ($isCount) {
        	return $res['total_found'];
        }
        
        $return = array();
        if(is_array($res['matches'])){
	        foreach ($res['matches'] as $v) {           
	            $return[] = $v;
	        }
        }
        return $return;		
	}
	
	public function searchKey($key, $type='any', $limit = 100)
	{
		require_once MY_ROOT . '/library/sphinxapi.php';
        $cl = new SphinxClient ();
        $cl->SetServer ('/var/run/searchd.sock');
        //$cl->SetServer ('localhost', 9312);
        $cl->SetConnectTimeout ( 2 );
        $cl->SetArrayResult ( true );
        $cl->SetLimits(0,  $limit);
        if ($type == 'any') {
        	$cl->SetMatchMode ( SPH_MATCH_ANY );  
        } else {
        	$cl->SetMatchMode ( SPH_MATCH_ALL );  
        }      
    
        $res = $cl->Query ($key, 'keywords');
        if ($res['total_found'] == 0) {
            return array();
        }
       
        $return = array();
        if(is_array($res['matches'])){
	        foreach ($res['matches'] as $v) {           
	            $return[] = $v['attrs'];
	        }
        }
        return $return;		
	}
	
	public function getKeywwordSearch($keywords){
		$keywords = trim(trim($keywords),',');
		$keywordArray = preg_split('/[\s,]/', $keywords);
		$newKeywordArray = array();
		foreach($keywordArray as $val){
			if(trim($val)!=''){
				array_push($newKeywordArray,$val);
			}
		}
		if(count($newKeywordArray)>=2){
			$keywords=$newKeywordArray[0].' '.$newKeywordArray[1];
		}else if(count($newKeywordArray) == 1){
			$keywords=$newKeywordArray[0];
		}
		if($keywords!=''){
			$idArray = $this->search($keywords,6);
			if(is_array($idArray)&&count($idArray)>0){
				$crawl = new CCrawl();
				return $crawl->getCrawlItems($idArray);
			}else{
				$maxCrawlId = CCache::getCrawlMaxId()+1;
				$idArray = array(mt_rand(1,$maxCrawlId),mt_rand(1,$maxCrawlId),mt_rand(1,$maxCrawlId),mt_rand(1,$maxCrawlId),mt_rand(1,$maxCrawlId),mt_rand(1,$maxCrawlId));
				$crawl = new CCrawl();
				return $crawl->getCrawlItems($idArray);
			}
		}
		return array();
	}
}